<?php
/**
 *  投诉类
 *
 */
namespace app\admin\controller;

use think\helper\Time;
use app\common\model\Competitor;

class Complaint extends Base
{
    protected $model;

    protected function initialize()
    {
        parent::initialize();
        $this->model = \app\common\model\Complaint::getInstance();
    }

    /**
     * 投诉记录
     * @return mixed
     * @throws \ip2region\Exception
     */
    public function lists()
    {
        $params = $this->request->param();
        $where = [];
        $keyword = isset($params['keyword']) ? $params['keyword'] : '';
        if (!empty($keyword)) {
            $where[] = ['a.title', 'like', '%' . urldecode($keyword) . '%'];
        }
        //时间选择
        $day = isset($params['day']) ? $params['day'] : 'all';
        //时间段选择
        $btime = isset($params['btime']) ? $params['btime'] : '';
        if (empty($btime)) {
            $params['day'] = $day;
            switch ($day) {
                case 'today':
                    list($start_time, $end_time) = Time::today();
                    $where[] = ['c.create_time', 'between', [$start_time, $end_time]];
                    break;
                case 'yestoday':
                    list($start_time, $end_time) = Time::yesterday();
                    $where[] = ['c.create_time', 'between', [$start_time, $end_time]];
                    break;
                case 'three':
                    list($start_time, $end_time) = Time::dayToNow(3);
                    $where[] = ['c.create_time', 'between', [$start_time, $end_time]];
                    break;
                case 'week':
                    list($start_time, $end_time) = Time::dayToNow(7);
                    $where[] = ['c.create_time', 'between', [$start_time, $end_time]];
                    break;
                default:
                    break;
            }
        } else {
            $btime_array = explode(' - ', $btime);
            $where[] = ['c.create_time', 'between', [strtotime($btime_array[0]), strtotime($btime_array[1]) + 86399]];
        }
        //时间选择
        $type = isset($params['type']) ? $params['type'] : '0';
        $info = [];
        $info['keyword'] = $keyword;
        $info['day'] = $day;
        $info['btime'] = $btime;
        $info['type'] = $type;

        $lists = $this->model->alias('c')
            ->where($where)
            ->field('c.*,a.title')
            ->join("activity a", "a.id = c.activity_id", 'LEFT')
            ->paginate(20, false, ['query' => $this->request->param()]);
        if (!empty($lists)) {
            $datas = $lists->toArray()['data'];
            $competitor_ids = array_column($datas, 'competitor_id');
            $competitors = Competitor::getInstance()->where('id', 'in', $competitor_ids)->column('id,name,remark', 'id');
            $ip_obj = new \ip2region\Ip2Region('ip2region.db');
            foreach ($lists as $k => $item) {
                $item['create_time'] = formatTime($item['create_time']);
                $ip_info = $ip_obj->binarySearch($item['ip']);
                $ip_array = explode('|', $ip_info['region']);
                $item['last_ip'] = isset($ip_info['region']) ? $item['ip'] . '/' . $ip_array[2] . $ip_array[3] . $ip_array[4] : $item['ip'];
                $item['competitor_name'] = isset($competitors[$item['competitor_id']]) ? $competitors[$item['competitor_id']]['name'] : '';
                $lists[$k] = $item;
            }
        }
        $this->assign('lists', $lists);
        $this->assign($info);
        return $this->fetch();
    }


}